package cn.kgc.eduservice.service.impl;

import cn.kgc.eduservice.entity.EduChapter;
import cn.kgc.eduservice.entity.EduVideo;
import cn.kgc.eduservice.entity.chapter.ChapterVo;
import cn.kgc.eduservice.entity.chapter.VideoVo;
import cn.kgc.eduservice.mapper.EduChapterMapper;
import cn.kgc.eduservice.service.EduChapterService;
import cn.kgc.eduservice.service.EduVideoService;
import cn.kgc.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-07-06
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {


    @Autowired
    private EduVideoService videoService;
    //课程大纲列表，根据课程id进行查询
    @Override
    public List<ChapterVo> getChapterVIdeoByCourseId(String courseId) {
        //根据课程id查询课程里面所有的章节
        QueryWrapper<EduChapter> wrapperChapter = new QueryWrapper<>();
        wrapperChapter.eq("course_id",courseId);
        List<EduChapter> eduChapterList = baseMapper.selectList(wrapperChapter);

        //根据课程id查询课程里面所有的小结
        QueryWrapper<EduVideo> wrapperVideo = new QueryWrapper<>();
        wrapperVideo.eq("course_id",courseId);
        List<EduVideo> eduVideoList = videoService.list(wrapperVideo);

        //用于最终封装数据
        List<ChapterVo> finalList = new ArrayList<>();

        // 遍历查询章节list集合进行封装
        for (int i = 0; i <eduChapterList.size() ; i++) {
            EduChapter eduChapter = eduChapterList.get(i);
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(eduChapter,chapterVo);
            finalList.add(chapterVo);
        //遍历查询小节list集合，进行封装
            // 用于封装章节的小结
            List<VideoVo> videoVoList = new ArrayList<>();

            for (int m = 0; m <eduVideoList.size() ; m++) {
                EduVideo eduVideo = eduVideoList.get(m);
                if (eduVideo.getChapterId().equals(eduChapter.getId())){
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(eduVideo,videoVo);
                    videoVoList.add(videoVo);
                }
            }
            chapterVo.setChildren(videoVoList);
        }

        return finalList;
    }

    @Override
    public boolean deleteChapter(String chapterId) {
        //根据chapterId 章节id 查询小结表，如果查询数据，不进行删除
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id",chapterId);
        int count = videoService.count(wrapper);
        //判断
        if(count >0){ // 查询出小节，不进行删除
            throw new GuliException(20001,"不能删除");
        }else { //不能查询数据，进行删除
            int result = baseMapper.deleteById(chapterId);
            return  result>0;
        }
    }

    @Override
    public void removeChapterByCourseId(String courseId) {
        QueryWrapper<EduChapter>wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);
    }
}
